import time
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from common.Utils import FroumDriver

class FroumHPage:
    driver=""
    url=""
    def __init__(self):
        self.driver=FroumDriver.driver
        #添加 隐式等待
        self.driver.implicitly_wait(5)
        self.url="http://127.0.0.1:9580/index.html"
        self.driver.get(self.url)

    #测试论坛首页元素
    def HPageTest(self):
        #检查用户头像是否存在
        self.driver.find_element(By.CSS_SELECTOR,"#index_nav_avatar")
        #检查搜索框是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.d-none.d-md-flex.me-3 > div > form > div > input")
        #检查论坛内容页是否存在
        self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content")
        #检查“发布帖子“按钮是否存在
        self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")
        #屏幕截图
        time.sleep(1)
        FroumDriver.getScreenShot()

    #论坛首页搜索框功能测试
    def HPageSearchTest(self):
        #（1）搜索关键词（由于搜索功能存在bug，搜索框不具备搜索功能，这里使用截图捕捉bug）
        search_button=self.driver.find_element(By.CSS_SELECTOR,"body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.d-none.d-md-flex.me-3 > div > form > div > input")
        search_button.send_keys("这是一个标题")
        time.sleep(1)
        #捕捉输入文本时的截图
        FroumDriver.getScreenShot()
        #模拟键盘回车输入
        search_button.send_keys(Keys.RETURN)
        time.sleep(1)
        FroumDriver.getScreenShot()

        #（2）搜索内容为空（直接选中搜索框回车搜索就可以）
        search_button=self.driver.find_element(By.CSS_SELECTOR,"body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.d-none.d-md-flex.me-3 > div > form > div > input")
        search_button.send_keys(Keys.RETURN)
        print("搜索空内容")
        time.sleep(1)
        FroumDriver.getScreenShot()












